//
//  ShareMessageViewController.m
//  Umoney
//
//  Created by zwz on 16/8/11.
//  Copyright © 2016年 zhangwz. All rights reserved.
//

#import "ShareMessageViewController.h"

@interface ShareMessageViewController ()<UITextViewDelegate,UITextFieldDelegate>
{
    NSString *UID;
    NSArray *res;
}
@property (strong, nonatomic) IBOutlet UITextView *shareText;

@end

@implementation ShareMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addGesture];
    // Do any additional setup after loading the view from its nib.
    [self SetTextVIew];
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    UID = [user objectForKey:@"uid"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)SetTextVIew
{
    self.shareText.layer.borderWidth = 2;
    self.shareText.layer.borderColor = [UIColor blackColor].CGColor;
    self.shareText.layer.cornerRadius = 3;
    self.shareText.layer.masksToBounds = YES;
    self.shareText.delegate = self;
}
- (IBAction)broadcastButton:(id)sender
{
    //发布
    //NSLog(@"%@",self.shareText.text);
    NSString *urlString = [NSString stringWithFormat:@"http://23.106.134.160/umoney/sharemessage.php?uid=%@&&message=%@",UID,self.shareText.text];
    
    NSString *encodeString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url=[NSURL URLWithString:encodeString];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    }];

    [self alter];
}

-(void)addGesture
{
    UITapGestureRecognizer *tap1=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewtaped:)];
    tap1.cancelsTouchesInView=NO;
    [self.view addGestureRecognizer:tap1];
}


-(void)viewtaped:(UIGestureRecognizer *)tap1
{
    [self.view endEditing:YES];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

-(void)alter
{
    UIAlertController *altervc = [UIAlertController alertControllerWithTitle:@"提示" message:@"分享成功" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        [self.navigationController popViewControllerAnimated:YES];
    }];
    [altervc addAction:confirm];
    
    [self presentViewController:altervc animated:NO completion:nil];
    
}

-(void)erroralter
{
    UIAlertController *altervc = [UIAlertController alertControllerWithTitle:@"提示" message:@"分享失败，密码错误" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
        [self.shareText setText:@"说点什么吧！"];
        [self.navigationController popToRootViewControllerAnimated:YES];
    }];
    [altervc addAction:confirm];
    
    [self presentViewController:altervc animated:NO completion:nil];
    
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
